/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package udp2serial;
import java.awt.Color;
import java.io.*;
import java.net.*;
import java.util.*;

/**
 *
 * @author axelsben
 */
public class server{

    private int useSocket = 4774;
    private javax.swing.JTextArea jta = null;
    private srvThread srvT = null;
    private boolean running = true;
    private String useAddress = "localhost";
    private boolean loopback = false;
    private boolean logging = false;
    private javax.swing.JTextField lSocket = null;
    private javax.swing.JTextField sSocket = null;
    private javax.swing.JTextField cAddr   = null;

    public void setUDPServer(javax.swing.JTextField listenOnSocket,javax.swing.JTextField sendOnSocket,javax.swing.JTextField clientAddress)
    {
        lSocket = listenOnSocket;
        sSocket = sendOnSocket;
        cAddr = clientAddress;
    }

    public void setLogging(boolean bool)
    {
        logging = bool;
    }
    public void setLoopback(boolean lb)
    {
        loopback = lb;
    }

    public void setTA(javax.swing.JTextArea jtxta)
    {
        jta = jtxta;
    }

    public void setSocket(int socket)
    {
        useSocket = socket;
    }

    public int getSocket()
    {
        return useSocket;
    }

    public void setAddress(String address)
    {
        useAddress = address;
    }

    public String getAddress()
    {
        return useAddress;
    }

    public void startServer () throws IOException
    {
        running = true;
        srvT = new srvThread();
        srvT.start();
    }

    public void stopServer()
    {
        running = false;
        srvT = null;
        System.gc();
    }

    public class srvThread extends Thread
    {
        protected DatagramSocket socket = null;

        public srvThread() throws IOException
        {
            this("srvThread");
        }

        public srvThread(String name) throws IOException
        {
            super(name);
            socket = new DatagramSocket(useSocket);
        }


        @Override
        public void run() {

            while (running) {
                try {
                    byte[] buf = new byte[256];
                    String[] args = new String[1];

                    // receive request
                    DatagramPacket packet = new DatagramPacket(buf, buf.length);
                    socket.receive(packet);
                    String got = new String(packet.getData(), 0, packet.getLength());
                    if(loopback)
                    {
                        // figure out response
                        //String dString = dString = new Date().toString();
                        args[0] = new String(got);
                        // send the response to the client at "address" and "port"
                        client.send(args);
                        jta.setSelectionStart(jta.getText().length());
                        jta.setSelectionEnd(jta.getText().length()+1);
                        jta.setSelectedTextColor(Color.blue);
                        jta.replaceSelection(got);
                        jta.setSelectedTextColor(Color.blue);
                    }
                    else
                    {
                        client.setAddress(packet.getAddress().getHostAddress() );
                        //client.setSocket(packet.getPort());
                        lSocket.setText("Listen on:" + useSocket);
                        sSocket.setText("Send to socket:" + client.getSocket() );
                        cAddr.setText("Send to address:" + client.getAddress());
                        args[0] = new String(got);
                        tx.send(args);
                        if(logging)
                        {
                            jta.setSelectionStart(jta.getText().length());
                            jta.setSelectionEnd(jta.getText().length()+1);
                            jta.replaceSelection("/ur:" + got);
                            if(jta.getText().length() > 25000)
                            {
                                int rm = jta.getText().length() - 25000;
                                jta.setSelectionStart(0);
                                jta.setSelectionEnd(rm);
                                jta.replaceSelection("");
                            }
                            jta.setSelectionStart(jta.getText().length());
                            jta.setSelectionEnd(jta.getText().length()+1);
                        }

                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}
